<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>批次实例信息列表</title>
<link href="../../static/plugin/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css" />
<link href="../../static/plugin/easyui/themes/icon.css" rel="stylesheet" type="text/css" />
<script src="../../static/plugin/easyui/jquery.min.js" type="text/javascript"></script>
<script src="../../static/plugin/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../static/plugin/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
<script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
<script src="../../ffres/dict/all.js" type="text/javascript"></script>
<a href="" target="_blank" id="commonLink" name="commonLink"></a>
</head>
<style>
	.task-tree-icon {
		background: url('../../static/img/tree/nodepic.gif') no-repeat 0
	}
</style>
<script type="text/javascript">
	$(function() {
		var batchId = $.getUrlParam('batchId');
		$("#taskTree").tree({
			url : '../../api/bat/inst/task/tree/' + batchId,
			method : 'get',
			animate : true,
			onSelect: function(node) {
				buttomDisable(node.attributes);
			},
			formatter:function(node){
				if(node.attributes) {
					node.iconCls = 'task-tree-icon';
					var taskInfo = node.attributes;
					return '<span style="color:' + getColor(taskInfo.taskRunState) + '">' + node.text + '</span>';
				} else {
					return node.text;
				}
			}
		});

		$("#dg").datagrid({
			singleSelect : true,
			collapsible : true,
			url : '../../api/bat/inst/tasks',
			queryParams: {
				batchId: batchId,
				startTimeNotNull: true
			},
			method : 'post',
			toolbar : "#tb",
			title : "Load Data",
			rownumbers : true,
			pagination : true,
			pageSize : "20"
		});
		
		timeRefresh(true);
	});
	
	function getColor(taskRunState) {
		var color = "";
		if(taskRunState == 7){
			color = "red";
		}else if(taskRunState == 2){
			color="#40a0ff";
		}else if(taskRunState == 4 || taskRunState == 3){
			color="#FFDA44";
		}else if(taskRunState == 1){
			color="#c000ff";
		}else if(taskRunState == 6){
			color="#00cc00";
		}else if(taskRunState == 0){
			color="#000000";
		}
		return color;
	}
	
	function buttomDisable(taskInfo){
		if(taskInfo.taskInterveneState == 0 && taskInfo.taskRunState < 6){
			$("#taskHangUp").linkbutton("enable");
		} else {
			$("#taskHangUp").linkbutton("disable");
		}
		
		if(taskInfo.taskInterveneState == 2){
			$("#taskHangDown").linkbutton("enable");
		} else {
			$("#taskHangDown").linkbutton("disable");
		}
		
		if(taskInfo.taskRunState != 6 && taskInfo.taskInterveneState != 1){
			$("#taskSkip").linkbutton("enable");
		} else {
			$("#taskSkip").linkbutton("disable");
		}
		
		if(taskInfo.taskRunState == 7){
			$("#taskAgainRun").linkbutton('enable');
		} else {
			$("#taskAgainRun").linkbutton('disable');
		}
		
		if((taskInfo.taskRunState == 2|| taskInfo.taskRunState == 3 || taskInfo.taskRunState == 4) && taskInfo.taskInterveneState != 3){
			$("#taskExit").linkbutton('enable');
		} else {
			$("#taskExit").linkbutton('disable');
		}
	}
	
	function timeRefresh(first) {
		if(!first) {
			$("#dg").datagrid('reload');
			$("#taskTree").tree('reload');
		}
		window.setTimeout("timeRefresh()",5*1000);
	}
	
	function doView(){
		var row = $('#dg').datagrid('getSelected');
		if (row) {
			JLEUtil.createModalWin('详情','BatInstTaskView.html?' + '&batchId=' + row.batchId + '&batchOrder=' + row.batchOrder + '&taskId=' + row.taskId);
		} else {
			$.messager.alert('提示','请选择一条记录！','info');
		}
	}
	
	//修改
	function doUpdateBatInstTask() {
		var node = $("#taskTree").tree('getSelected');
		if(node && node.attributes) {
			JLEUtil.createModalWin('修改','BatInstTaskUpdate.html?' + '&batchId=' + node.attributes.batchId + '&batchOrder='
					+ node.attributes.batchOrder + '&taskId=' + node.attributes.taskId);
		} else {
			$.messager.alert('提示','请选择一条任务！','info');
		}
	}
	
	//挂起
	function doTaskHangUp() {
		var node = $("#taskTree").tree('getSelected');
		if(node && node.attributes) {
			$.messager.confirm('提示', '是否确认要挂起任务？', function(r){
				if(r) {
					var taskInfo = node.attributes;
					$.restfulPut('../../api/bat/inst/task/hangup', taskInfo, function(data){
						if(data.code == 0) {
							$.messager.alert('提示','挂起成功！','success');
						} else {
							$.messager.alert('提示','挂起失败！','error');
						}
					})
				}
			});
		} else {
			$.messager.alert('提示','请选择一条任务！','info');
		}
	}
	
	//解挂
	function doTaskHangDown() {
		var node = $("#taskTree").tree('getSelected');
		if(node && node.attributes) {
			$.messager.confirm('提示', '是否确认要解挂任务？', function(r){
				if(r) {
					var taskInfo = node.attributes;
					$.restfulPut('../../api/bat/inst/task/hangdown', taskInfo, function(data){
						if(data.code == 0) {
							$.messager.alert('提示','解挂成功！','success');
						} else {
							$.messager.alert('提示','解挂失败！','error');
						}
					})
				}
			});
		} else {
			$.messager.alert('提示','请选择一条任务！','info');
		}
	}
	
	//置过
	function doTaskSkip() {
		var node = $("#taskTree").tree('getSelected');
		if(node && node.attributes) {
			$.messager.confirm('提示', '是否确认要置过任务？', function(r){
				if(r) {
					var taskInfo = node.attributes;
					$.restfulPut('../../api/bat/inst/task/skip', taskInfo, function(data){
						if(data.code == 0) {
							$.messager.alert('提示','置过成功！','success');
						} else {
							$.messager.alert('提示','置过失败！','error');
						}
					})
				}
			});
		} else {
			$.messager.alert('提示','请选择一条任务！','info');
		}
	}
	
</script>
</head>
<body class="easyui-layout" style="padding: 10px">
	<div data-options="region:'north',split:true" style="width: 100%; height: 6%; background: #F4F4F4;">
		<div style="margin-bottom:2px">
			<a href="javascript:doUpdateBatInstTask()" id="updateBatInstTask" class="easyui-linkbutton" iconCls="icon-ok" plain="true">修改</a>
			<a href="javascript:doTaskHangUp()" id="taskHangUp" class="easyui-linkbutton" iconCls="icon-ok" plain="true">挂起</a>
			<a href="javascript:doTaskHangDown()" id="taskHangDown" class="easyui-linkbutton" iconCls="icon-ok" plain="true">解挂</a>
			<a href="javascript:doTaskSkip()" id="taskSkip" class="easyui-linkbutton" iconCls="icon-ok" plain="true">置过</a>
		    <a href="javascript:doTaskAgainRun()" id="taskAgainRun" class="easyui-linkbutton" iconCls="icon-ok" plain="true" style="display:none;">重跑</a>
			<a href="javascript:doTaskExit()" id="taskExit" class="easyui-linkbutton" iconCls="icon-ok" plain="true" style="display:none;">退出</a>
			
			<span style="color:#40a0ff">蓝色:执行中 </span>
			<span style="color:#000000"> | </span>
			<span style="color:#c000ff"> 紫色:待执行 </span>
			<span style="color:#000000"> | </span>
			<span style="color:#00cc00"> 绿色:执行成功 </span>
			<span style="color:#000000"> | </span>
			<span style="color:#FFDA44"> 橙色:警告 </span>
			<span style="color:#000000"> | </span>
			<span style="color:red"> 红色:执行失败 </span>
		</div>
	</div>
	<div data-options="region:'west',split:true"
		style="height: 100%; width: 20%; background: #F4F4F4;">
		<ul id="taskTree"></ul>
	</div>
	<div data-options="region:'center',split:true" style="height: 100%; width: 80%;">
		<table id="dg" style="height: 100%;">
			<thead>
				<tr>
					<th data-options="field:'taskId',resizable:true">任务编号</th>
					<th data-options="field:'taskName',resizable:true">任务名称</th>
					<th data-options="field:'taskRunState',resizable:true"
						formatter="formatter_BAT_TASK_RUN_STATE">任务执行状态</th>
					<th data-options="field:'taskInterveneState',resizable:true"
						formatter="formatter_BAT_TASK_INTERVENE_STATE">任务干预状态</th>
					<th data-options="field:'startTime',resizable:true">启动时间</th>
					<th data-options="field:'endTime',resizable:true">结束时间</th>
					<th data-options="field:'costTime',resizable:true">运行时长（秒）</th>
				</tr>
			</thead>
		</table>
	</div>
	<div id="tb" style="padding:1px">
		<div style="margin-bottom:2px">
			<a href="javascript:doView()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">查看</a>
		</div>
	</div>

</body>
</html>
